<html>
<head><meta charset="utf-8"><title>Atomic ptr ops? · t-compiler/wg-llvm · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/187780-t-compiler/wg-llvm/index.html">t-compiler/wg-llvm</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/187780-t-compiler/wg-llvm/topic/Atomic.20ptr.20ops.3F.html">Atomic ptr ops?</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="192945376"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187780-t-compiler/wg-llvm/topic/Atomic%20ptr%20ops%3F/near/192945376" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/187780-t-compiler/wg-llvm/topic/Atomic.20ptr.20ops.3F.html#192945376">(Apr 05 2020 at 06:56)</a>:</h4>
<p>In LLVM, is it possible to do atomic operations (whatever we are using to lower the <code>atomic_store</code> et al intrinsics) on pointer type, or does it have to be an integer type?</p>



<a name="192949710"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187780-t-compiler/wg-llvm/topic/Atomic%20ptr%20ops%3F/near/192949710" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/187780-t-compiler/wg-llvm/topic/Atomic.20ptr.20ops.3F.html#192949710">(Apr 05 2020 at 09:09)</a>:</h4>
<p>I don't see any indication in the LLVM lang ref that says it can't be a pointer. In fact it seems that arbitrary types are allowed, with atomic ops for too big types lowered to <code>__atomic_*</code> calls, but I am not sure about this.</p>



<a name="192950923"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187780-t-compiler/wg-llvm/topic/Atomic%20ptr%20ops%3F/near/192950923" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/187780-t-compiler/wg-llvm/topic/Atomic.20ptr.20ops.3F.html#192950923">(Apr 05 2020 at 09:42)</a>:</h4>
<p>the docs indicate the types as <code>iN</code> though, dont they?</p>



<a name="192950926"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187780-t-compiler/wg-llvm/topic/Atomic%20ptr%20ops%3F/near/192950926" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/187780-t-compiler/wg-llvm/topic/Atomic.20ptr.20ops.3F.html#192950926">(Apr 05 2020 at 09:42)</a>:</h4>
<p>(currently, rustc complains when such an intrinsic is used with a non-integer type)</p>



<a name="192952038"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187780-t-compiler/wg-llvm/topic/Atomic%20ptr%20ops%3F/near/192952038" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/187780-t-compiler/wg-llvm/topic/Atomic.20ptr.20ops.3F.html#192952038">(Apr 05 2020 at 10:11)</a>:</h4>
<p>Take for example <a href="https://llvm.org/docs/LangRef.html#cmpxchg-instruction" title="https://llvm.org/docs/LangRef.html#cmpxchg-instruction">https://llvm.org/docs/LangRef.html#cmpxchg-instruction</a>. The type  is <code>&lt;ty&gt;</code>. It never says <code>iN</code>.</p>



<a name="192957237"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187780-t-compiler/wg-llvm/topic/Atomic%20ptr%20ops%3F/near/192957237" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Amanieu <a href="https://rust-lang.github.io/zulip_archive/stream/187780-t-compiler/wg-llvm/topic/Atomic.20ptr.20ops.3F.html#192957237">(Apr 05 2020 at 12:30)</a>:</h4>
<p><span class="user-mention" data-user-id="120791">@RalfJ</span> Only <code>atomicrmw</code> has restrictions on the type: <a href="https://llvm.org/docs/LangRef.html#atomicrmw-instruction" title="https://llvm.org/docs/LangRef.html#atomicrmw-instruction">https://llvm.org/docs/LangRef.html#atomicrmw-instruction</a></p>



<a name="192982903"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187780-t-compiler/wg-llvm/topic/Atomic%20ptr%20ops%3F/near/192982903" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/187780-t-compiler/wg-llvm/topic/Atomic.20ptr.20ops.3F.html#192982903">(Apr 05 2020 at 23:13)</a>:</h4>
<p><span class="user-mention" data-user-id="143274">@Amanieu</span> hm, so we should be able to make <code>AtomicPtr</code> lower to ptr-typed LLVM ops?</p>



<a name="192982906"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187780-t-compiler/wg-llvm/topic/Atomic%20ptr%20ops%3F/near/192982906" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/187780-t-compiler/wg-llvm/topic/Atomic.20ptr.20ops.3F.html#192982906">(Apr 05 2020 at 23:13)</a>:</h4>
<p>ah no we probably use <code>atomicrmw</code> for some of those</p>



<a name="192982907"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187780-t-compiler/wg-llvm/topic/Atomic%20ptr%20ops%3F/near/192982907" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Amanieu <a href="https://rust-lang.github.io/zulip_archive/stream/187780-t-compiler/wg-llvm/topic/Atomic.20ptr.20ops.3F.html#192982907">(Apr 05 2020 at 23:13)</a>:</h4>
<p>Let me check what clang does, we should probably just do whatever they do.</p>



<a name="192983005"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187780-t-compiler/wg-llvm/topic/Atomic%20ptr%20ops%3F/near/192983005" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/187780-t-compiler/wg-llvm/topic/Atomic.20ptr.20ops.3F.html#192983005">(Apr 05 2020 at 23:16)</a>:</h4>
<p>well the context here is <a href="https://github.com/rust-lang/rust/pull/70765" title="https://github.com/rust-lang/rust/pull/70765">https://github.com/rust-lang/rust/pull/70765</a> which clang probably is not much concerned with</p>



<a name="192983073"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187780-t-compiler/wg-llvm/topic/Atomic%20ptr%20ops%3F/near/192983073" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Amanieu <a href="https://rust-lang.github.io/zulip_archive/stream/187780-t-compiler/wg-llvm/topic/Atomic.20ptr.20ops.3F.html#192983073">(Apr 05 2020 at 23:19)</a>:</h4>
<p><span class="user-mention" data-user-id="120791">@RalfJ</span> clang just bitcasts pointers to integers.</p>



<a name="192983076"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187780-t-compiler/wg-llvm/topic/Atomic%20ptr%20ops%3F/near/192983076" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/187780-t-compiler/wg-llvm/topic/Atomic.20ptr.20ops.3F.html#192983076">(Apr 05 2020 at 23:19)</a>:</h4>
<p>so thats more a transmute than an <code>as usize</code>?</p>



<a name="192983123"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187780-t-compiler/wg-llvm/topic/Atomic%20ptr%20ops%3F/near/192983123" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Amanieu <a href="https://rust-lang.github.io/zulip_archive/stream/187780-t-compiler/wg-llvm/topic/Atomic.20ptr.20ops.3F.html#192983123">(Apr 05 2020 at 23:20)</a>:</h4>
<p>Can't miri just track provenance through casts just like transmute?</p>



<a name="192983203"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187780-t-compiler/wg-llvm/topic/Atomic%20ptr%20ops%3F/near/192983203" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Amanieu <a href="https://rust-lang.github.io/zulip_archive/stream/187780-t-compiler/wg-llvm/topic/Atomic.20ptr.20ops.3F.html#192983203">(Apr 05 2020 at 23:23)</a>:</h4>
<p>Honestly I agree with Aaron here, casts and transmutes should be treated identically because the difference is too subtle otherwise.</p>



<a name="192983209"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187780-t-compiler/wg-llvm/topic/Atomic%20ptr%20ops%3F/near/192983209" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/187780-t-compiler/wg-llvm/topic/Atomic.20ptr.20ops.3F.html#192983209">(Apr 05 2020 at 23:23)</a>:</h4>
<p>I dont think that would be correct. Rust's ptr provenance rules are unclear but if they are anything like C's or LLVM's, casting to an int is spec'd to lose provenance.<br>
Also, in general no it cannot -- integers have way too many operations to track provenance on them.</p>



<a name="192983251"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187780-t-compiler/wg-llvm/topic/Atomic%20ptr%20ops%3F/near/192983251" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/187780-t-compiler/wg-llvm/topic/Atomic.20ptr.20ops.3F.html#192983251">(Apr 05 2020 at 23:24)</a>:</h4>
<p><span class="user-mention silent" data-user-id="143274">Amanieu</span> <a href="#narrow/stream/187780-t-compiler.2Fwg-llvm/topic/Atomic.20ptr.20ops.3F/near/192983203" title="#narrow/stream/187780-t-compiler.2Fwg-llvm/topic/Atomic.20ptr.20ops.3F/near/192983203">said</a>:</p>
<blockquote>
<p>Honestly I agree with Aaron here, casts and transmutes should be treated identically because the difference is too subtle otherwise.</p>
</blockquote>
<p>I'm afraid that's <a href="https://people.mpi-sws.org/~jung/twinsem/twinsem.pdf" title="https://people.mpi-sws.org/~jung/twinsem/twinsem.pdf">not possible</a>. This is not a Miri thing, this is about how to define a semantics that actually makes all our optimizations legal.</p>



<a name="192983259"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187780-t-compiler/wg-llvm/topic/Atomic%20ptr%20ops%3F/near/192983259" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/187780-t-compiler/wg-llvm/topic/Atomic.20ptr.20ops.3F.html#192983259">(Apr 05 2020 at 23:25)</a>:</h4>
<p>LLVM devs seem to think casts and transmutes are the same and <a href="https://bugs.llvm.org/show_bug.cgi?id=34548" title="https://bugs.llvm.org/show_bug.cgi?id=34548">it leads to miscompilations</a>.</p>



<a name="192983263"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187780-t-compiler/wg-llvm/topic/Atomic%20ptr%20ops%3F/near/192983263" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/187780-t-compiler/wg-llvm/topic/Atomic.20ptr.20ops.3F.html#192983263">(Apr 05 2020 at 23:25)</a>:</h4>
<p>and <a href="https://bugs.llvm.org/show_bug.cgi?id=35229" title="https://bugs.llvm.org/show_bug.cgi?id=35229">more miscompilations</a></p>



<a name="192983267"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187780-t-compiler/wg-llvm/topic/Atomic%20ptr%20ops%3F/near/192983267" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/187780-t-compiler/wg-llvm/topic/Atomic.20ptr.20ops.3F.html#192983267">(Apr 05 2020 at 23:25)</a>:</h4>
<p>GCC <a href="https://gcc.gnu.org/bugzilla/show_bug.cgi?id=82282" title="https://gcc.gnu.org/bugzilla/show_bug.cgi?id=82282">has the same bugs</a></p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>